package com.starryx.websocket.chatroom.net.session;

import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component("session")
public class Session {

    private Map<String, Channel> channelMap = new HashMap<>();

    private Map<Channel, String> nameMap = new HashMap<>();

    public Channel getChannel(String name) {
        return channelMap.get(name);
    }

    public String getName(Channel channel) {
        return nameMap.get(channel);
    }

    public void put(String name, Channel channel) {
        channelMap.put(name, channel);
        nameMap.put(channel, name);
    }

    public boolean isLogin(String name) {
        return channelMap.containsKey(name);
    }

    public boolean isLogin(Channel channel) {
        return nameMap.containsKey(channel);
    }

    public void remove(String name) {
        channelMap.remove(name);
        nameMap.remove(channelMap.get(name));
    }

    public void remove(Channel channel) {
        nameMap.remove(channel);
        channelMap.remove(nameMap.get(channel));
    }

}
